<?php
/**
 * Created by ljhint on 2021/2/24.
 */

namespace App\Console\Commands;


use App\Models\VehicleBrand;
use App\Models\VehicleConfigure;
use App\Models\VehicleModel;
use App\Models\VehiclePick;
use App\Models\VehicleStyle;
use Illuminate\Console\Command;

class ProcessingData extends Command
{
    public $signature = 'Data {type}';
    protected $description = '处理数据';

    public function handle(){
        if($this->argument('type') == 'pickcar') {
            $this->setPick();
        }elseif ($this->argument('type') == 'brandImg'){
            $this->brandImg();
        }elseif ($this->argument('type') == 'vehicleImg'){
            $brand = VehicleBrand::all();
            foreach ($brand as $v){
                $this->vehicleImg($v->brandId);
            }
        }else{
            $this->error('Something went wrong!');
        }
        $this->info('ok!');
    }

    public function setPick()
    {
        $model = VehicleModel::all();
        foreach ($model as $value){
            $country = VehicleBrand::where('brandId',$value->brandId)->value('countryid');
            $style = VehicleStyle::where('seriesId',$value->seriesId)->where('state','<=',30)->where('minprice','<>',0.00)->where('maxprice','<>',0.00);
            $minprice = $style->min('minprice');
            $maxprice = $style->max('maxprice');
            $spec = $style->orderBy('specId', 'DESC')->first();
            if ($spec){
                $configure = VehicleConfigure::where('specId',$spec->specId)->where('param','<>','[]')->where('param','<>','null')->orderBy('styleIds', 'DESC')->first();
                if ($configure){
                    $level = $carbody = '';
                    foreach (json_decode($configure->param,true) as $item){
                        if($item['name'] == '基本参数'){
                            foreach ($item['paramitems'] as $v){
                                if($v['name'] == '级别') $level = $v['valueitems']['0']['value'];
                                if($v['name'] == '车身结构') $carbody = $v['valueitems']['0']['value'];
                            }
                            break;
                        }
                    }
                    VehiclePick::updateOrCreate(
                        ['seriesId'=>$value->seriesId],
                        [ 'country'=>$country, 'minprice'=>$minprice,'maxprice'=>$maxprice,'level'=>$level,'carbody'=>$carbody]
                    );
                }
            }
        }
    }

    public function brandImg()
    {
        $url = 'https://brand.baiche.com.cn/vehicle/';
        $result = $this->getUrl($url);
        $array = json_decode($result,true);
        if ($array['code'] == '200'){
            foreach ($array['result'] as $letter) {
                foreach ($letter as $v){
                    if($v['logoUrl']<>''){
                        if (VehicleBrand::where('brandId',$v['brandId'])->value('logoUrl') == NULL){
                            VehicleBrand::where('brandId',$v['brandId'])->update(['logoUrl' => $v['logoUrl']]);
                        }
                    }
                }
            }
        }
    }

    public function vehicleImg($brandId)
    {
        $url = 'https://brand.baiche.com.cn/vehicle/?b='.$brandId;
        $result = $this->getUrl($url);
        $array = json_decode($result,true);
        if ($array['code'] == '200'){
            foreach ($array['result'] as $v) {
                if($v['defaultImg']<>''){
                    if (VehicleModel::where('seriesId',$v['seriesId'])->value('defaultImg') == NULL){
                        if ($v['defaultImg']){
                            VehicleModel::where('seriesId',$v['seriesId'])->update(['defaultImg' => $v['defaultImg']]);
                        }
                    }
                }

                if($v['specimenImg']<>''){
                    if (VehicleModel::where('seriesId',$v['seriesId'])->value('specimenImg') == NULL){
                        if ($v['specimenImg']){
                            VehicleModel::where('seriesId',$v['seriesId'])->update(['specimenImg' => $v['specimenImg']]);
                        }
                    }
                }
            }
        }
    }

    protected function getUrl($url){
        $header = array (
            "Content-Type: application/json",
            "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7"
        );
        $ch = curl_init ();
        curl_setopt ($ch, CURLOPT_URL, $url );
        curl_setopt ($ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );
        $content = curl_exec ( $ch );
        if ($content == FALSE) {
            return curl_error ( $ch );
        }
        curl_close ( $ch );
        return $content;
    }

}
